package com.suieqian.filter.authFile;

import com.suieqian.config.IgnoreUrlsConfig;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


/**
 * 白名单验证
 *
 * @author win10
 */
@Component
public class IgnorePathHelper {

    @Resource
    private IgnoreUrlsConfig ignoreUrlsConfig;

    private AntPathMatcher pathMatcher;

    @PostConstruct
    private void init() {
        pathMatcher = new AntPathMatcher();
    }


    /**
     * Determines if a request matches or not
     *
     * @param request
     * @return
     */
    public boolean matches(HttpServletRequest request) {
        String currPath = request.getRequestURI();
        return ignoreUrlsConfig.getUrls().parallelStream().anyMatch(it -> pathMatcher.matchStart(it, currPath));
    }
}
